uniform sampler2D imageTex;
uniform float pointRadius;  // point size in world space
uniform float pointScale;   // scale to calculate size in pixels
uniform float opacityThreshold = 0.95;
uniform vec4 eyePos;
void main()
{
    // transform to clip space
    vec4 hpos = gl_ModelViewProjectionMatrix * gl_Vertex;
    // transform to window space
    vec2 wpos = (hpos.xy / hpos.w)*0.5+0.5;

    // cull point based on previous layer opacity
    float opacity = tex2D(imageTex, wpos).a;
    if (opacity > opacityThreshold) {
        hpos.w = -1.0;	// cull point
    }

    gl_Position = hpos;

    // calculate window-space point size
    vec4 eyeSpacePos = gl_ModelViewMatrix * gl_Vertex;
    float dist = length(eyeSpacePos.xyz);
    gl_PointSize = pointRadius * (pointScale / dist);

    gl_TexCoord[0] = gl_MultiTexCoord0; // sprite texcoord
    gl_TexCoord[1] = eyeSpacePos;
 
    gl_FrontColor = gl_Color;
}